// pages/results/results/result.js
var app = getApp()
var timeUtil = require('../../../utils/timeUtils')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    score: 0,
    done: 0,
    wrongText: "",
    wrongItems: [],
    time: ''
  },


  practice: function () {
    if (this.data.wrongNum == 0) {
      wx.showToast({
        title: "没有错题哦(●'◡'●)ﾉ♥",
      })
    } else {
      app.globalData.practiceFlag = true
      app.globalData.ButtomFlag = false
      app.globalData.practiceWrong = this.data.wrongItems
      wx.navigateTo({
        url: '../../sub/learns/learn',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title:  app.globalData.selectLib
    })
    var score = Math.round((app.globalData.done / app.globalData.total) * 100)
    this.setData({
      total: app.globalData.total,
      done: app.globalData.done,
      score: score,
      wrongText: app.globalData.wrongText,
      wrongItems: app.globalData.wrongItems,
      time: timeUtil.formatTime(app.globalData.time / 1000)
    })
    //在这里将做错的考卷提交到数据库
    let item = wx.getStorageSync('user')
      var myScore = (this.data.total-this.data.wrongItems.length)/this.data.total*100
      var intScore = Math.round(myScore)
      wx.cloud.database().collection("testRecords").add({
        data: {
          account: item.account,
          selectLib: app.globalData.selectLib,
          wrongItems: this.data.wrongItems,
          time: Date.now(),//这里应该上传的是时间戳
          total: this.data.total,
          score:intScore
        }
      }).then(res=>{
        console.log("用户考卷新增成功",res)
      }).catch(err=>{
        console.log("用户考卷新增失败",err)
      })
  },

  //当该页面销毁时，需要重新将app.globalData中的prcaticeFlag修改为false
  onUnload() {
    app.globalData.prcaticeFlag = false
  }
})